package cn.xianyang.product.service.impl;

import cn.xianyang.basic.service.impl.BaseServiceImpl;
import cn.xianyang.basic.util.BaiduAuditUtils;
import cn.xianyang.product.domain.Product;
import cn.xianyang.product.domain.ProductDetail;
import cn.xianyang.product.domain.ProductOnlineAuditLog;
import cn.xianyang.product.mapper.ProductDetailMapper;
import cn.xianyang.product.mapper.ProductMapper;
import cn.xianyang.product.mapper.ProductOnlineAuditLogMapper;
import cn.xianyang.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xianyang
 * @since 2022-06-29
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail productDetail = product.getDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetail.setOrderNotice(product.getDetail().getOrderNotice());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void onSale(List<Long> ids) {
        // 1 审核宠物
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            // 审核宠物名字
            StringBuilder sb = new StringBuilder();
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                sb.append(product.getName()+"名字含有敏感字符，请重新输入");
            }
            // 审核宠物图片
            if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+product.getResources().split(",")[0])){
                sb.append(product.getName()+"图片不合法，请重新选取");
            }
            //审核不通过 。记录日志 ，跳过下一个继续
            if (sb.toString().length()>0){
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setNote(sb.toString());
                productOnlineAuditLogMapper.save(log);
                continue;
            }
            //审核通过 ，修改上架状态，记录上架时间
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            productMapper.onSale(map);
        }
    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        productMapper.offSale(map);
    }








    @Override
    public void update(Product product) {

    }


    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }
}
